package reponse

import (
	"github.com/gin-gonic/gin"
	"net/http"
)

const (
	ERROR   = 500
	SUCCESS = 200
)

var null = map[string]interface{}{}

type Response struct {
	Code int         `json:"code"`
	Data interface{} `json:"data"`
	Msg  string      `json:"msg"`
}

func Result(code int, data interface{}, msg string, c *gin.Context) {
	c.JSON(http.StatusOK, &Response{
		Code: code,
		Data: data,
		Msg:  msg,
	})
}

// 成功的
func OK(c *gin.Context) {
	Result(SUCCESS, null, "操作成功", c)
}

func OKWithMsg(msg string, c *gin.Context) {
	Result(SUCCESS, null, msg, c)
}

func OKWithData(data interface{}, c *gin.Context) {
	Result(SUCCESS, data, "操作成功", c)
}

func OKWithDatailed(data interface{}, msg string, c *gin.Context) {
	Result(SUCCESS, data, msg, c)
}

// 错误的
func Fail(c *gin.Context) {
	Result(ERROR, null, "操作失败", c)
}

func FailWithMsg(msg string, c *gin.Context) {
	Result(ERROR, null, msg, c)
}

func FailWithDatailed(data interface{}, msg string, c *gin.Context) {
	Result(ERROR, data, msg, c)
}

func FailWithErr(err error, c *gin.Context) {
	Result(ERROR, null, err.Error(), c)
}
